﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace SteamGameApp
{
    static class DataManager<T>
    {

        public static ObservableCollection<T> Load(string filename)
        {
            try
            {
                XmlSerializer deserializer = new XmlSerializer(typeof(ObservableCollection<T>));
                TextReader textReader = new StreamReader(filename);
                ObservableCollection<T> loadedItems;
                loadedItems = (ObservableCollection<T>)deserializer.Deserialize(textReader);
                textReader.Close();
                return loadedItems;
            }
            catch
            {
                if(!File.Exists(filename))
                     File.Create(filename);
            }

            return new ObservableCollection<T>();

        }
        public static void Save(object obj, string filename)
        {
            TextWriter writer = new StreamWriter(filename);
            try
            {
                XmlSerializer sr = new XmlSerializer(obj.GetType());
                
                sr.Serialize(writer, obj);
            }
            catch
            {
            }
            finally
            {
                writer.Close();
            }
        }
    }
}
